<?php


namespace app\api\model\User;


use think\facade\Db;
use think\Model;

class UserWalletModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "user_wallet";

    /**
     * 根据条件查询单挑数据
     * @param $where
     * @param string $filed
     * @return array|Model|null
     * @throws
     */
    public static function getUserWalletWhere($where,$filed = "*")
    {
        $user = Db::name("user_wallet")->field($filed)->where($where)->find();
        return $user;
    }

    /**
     * 添加返回ID
     * @param $data
     * @return int
     */
    public static function addUserWallet($data)
    {
        $id = Db::name('user_wallet')->insertGetId($data);
        return $id;
    }

    /**
     * 统计
     * @param $where
     * @return float
     */
    public static function getUserWalletBillSum($where)
    {
        $amount = Db::name('user_amount_bill')->where($where)->sum('amount');
        return $amount;
    }


    /**
     * 用户余额入账
     * @param $uid
     * @param $amount
     * @param $bill_use_type
     * @param $remark
     * @param int $order_goods_id
     * @return bool
     * @throws
     */
    public static function walletIn($uid, $amount, $bill_use_type ,$remark, $order_goods_id = 0)
    {
        $flag = Db::name('user_wallet')->where([['uid','=',$uid]])->inc('amount', $amount)->update();
        if (!$flag) {
            return false;
        }
        $data = [
            'uid'            => $uid,
            'bill_type'      => 1,
            'amount'         => $amount,
            'remark'         => $remark,
            'bill_use_type'  => $bill_use_type,
            'add_time'       => date("Y-m-d H:i:s",time()),
            'order_goods_id' => $order_goods_id
        ];
        $result = Db::name('user_amount_bill')->insertGetId($data);
        if (!$result) {
            return false;
        }
        return true;
    }


    /**
     * 用户余额出账
     * @param $uid
     * @param $amount
     * @param $bill_use_type
     * @param string $remark
     * @return bool
     * @throws
     */
    public static function walletOut($uid, $amount, $bill_use_type ,$remark = '')
    {
        $flag = Db::name('user_wallet')->where([['uid','=',$uid]])->dec('amount', $amount)->update();
        if (!$flag) {
            return false;
        }
        $data = [
            'uid'           => $uid,
            'bill_type'     => 5,
            'amount'        => $amount,
            'remark'        => $remark,
            'bill_use_type' => $bill_use_type,
            'add_time'      => date("Y-m-d H:i:s",time()),
        ];
        $result = Db::name('user_amount_bill')->insertGetId($data);
        if (!$result) {
            return false;
        }
        return true;
    }


    /**
     * 余额入账
     * @param int $uid
     * @param string $amount
     * @param int $bill_use_type
     * @param string $remark
     * @return bool
     * @throws
     */
    public static function studyAmountIn(int $uid, string $amount, int $bill_use_type ,string $remark): bool
    {
        $flag = Db::name('user_wallet')->where([['uid','=',$uid]])->inc('amount', $amount)->update();
        if (!$flag) {
            return false;
        }
        $data = [
            'uid'           => $uid,
            'bill_type'     => 1,
            'amount'        => $amount,
            'remark'        => $remark,
            'bill_use_type' => $bill_use_type,
            'add_time'      => date("Y-m-d H:i:s",time()),
        ];
        $result = Db::name('user_study_amount_bill')->insertGetId($data);
        if (!$result) {
            return false;
        }
        return true;
    }


    /**
     * 学习金出账
     * @param int $uid
     * @param string $amount
     * @param int $bill_use_type
     * @param string $remark
     * @return bool
     * @throws
     */
    public static function studyAmountOut(int $uid, string $amount, int $bill_use_type ,string $remark = ''): bool
    {
        $flag = Db::name('user_wallet')->where([['uid','=',$uid]])->dec('study_amount', $amount)->update();
        if (!$flag) {
            return false;
        }
        $data = [
            'uid'           => $uid,
            'bill_type'     => 5,
            'amount'        => $amount,
            'remark'        => $remark,
            'bill_use_type' => $bill_use_type,
            'add_time'      => date("Y-m-d H:i:s",time()),
        ];
        $result = Db::name('user_study_amount_bill')->insertGetId($data);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 用户积分入账
     * @param int $uid
     * @param int $integral
     * @param int $bill_use_type
     * @param string $remark
     * @return bool
     * @throws
     */
    public static function integralIn(int $uid, int $integral, int $bill_use_type ,string $remark): bool
    {
        $flag = Db::name('user_wallet')->where([['uid','=',$uid]])->inc('integral', $integral)->update();
        if (!$flag) {
            return false;
        }
        $data = [
            'uid'           => $uid,
            'bill_type'     => 1,
            'integral'        => $integral,
            'remark'        => $remark,
            'bill_use_type' => $bill_use_type,
            'add_time'      => date("Y-m-d H:i:s",time()),
        ];
        $result = Db::name('user_integral_bill')->insertGetId($data);
        if (!$result) {
            return false;
        }
        return true;
    }

    /**
     * 用户积分出账
     * @param string $uid
     * @param float $integral
     * @param int $bill_use_type
     * @param string $remark
     * @return bool
     * @throws
     */
    public static function integralOut(string $uid, float $integral, int $bill_use_type ,string $remark = ''): bool
    {
        $flag = Db::name('user_wallet')->where([['uid','=',$uid]])->dec('integral', $integral)->update();
        if (!$flag) {
            return false;
        }
        $data = [
            'uid'           => $uid,
            'bill_type'     => 5,
            'integral'        => $integral,
            'remark'        => $remark,
            'bill_use_type' => $bill_use_type,
            'add_time'      => date("Y-m-d H:i:s",time()),
        ];
        $result = Db::name('user_integral_bill')->insertGetId($data);
        if (!$result) {
            return false;
        }
        return true;
    }


    /**
     * 用户推荐人数
     * @param $uid
     * @param $amount
     * @param $bill_use_type
     * @param $remark
     * @param int $order_goods_id
     * @return bool
     * @throws
     */
    public static function referralsIn($uid)
    {
        $flag = Db::name('user_wallet')->where([['uid','=',$uid]])->inc('referrals', 1)->update();
        if (!$flag) {
            return false;
        }
        return true;
    }

}